/*
 *  Borum, Boolean rule manipulator.
 *  Copyright (C) 2009 Jon Lennersten, Johan Hedin, Daniel Rosenberg
 *
 *  This file is part of Borum.
 *
 *  Borum is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Borum is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Borum.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.mawell.borum.demo;

import com.mawell.borum.Term;
import com.toedter.calendar.JDateChooser;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

@Root
public abstract class DateTerm extends Term {

    @Element
    private Date date = new Date();
    @Element
    private String compare = "=";

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getCompare() {
        return compare;
    }

    public void setCompare(String compare) {
        this.compare = compare;
    }

    public ExampleTranslation getTranslation() {
        if(getTranslationForTerm() != null && getTranslationForTerm() instanceof ExampleTranslation) {
            return (ExampleTranslation)getTranslationForTerm();
        } else {
            return new ExampleTranslation();
        }
    }

    public String toString() {
        //TODO make configurable
        DateFormat s = DateFormat.getDateInstance(DateFormat.MEDIUM, new Locale("sv","SE"));
        return getName() +' '+compare+' '+s.format(date);
    }

    public boolean edit(Component comp) {
        String[] options = {getTranslation().getLabelSave(), getTranslation().getLabelCancel()};
        EditPanel panel = new EditPanel(date);
        int result = JOptionPane.showOptionDialog(comp, panel, getName(), JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, getTranslation().getLabelSave());
        switch(result) {
            case 0:
                //Save pushed, save values and return true
                date = panel.getDc().getDate();
                compare = panel.getNewCompare();
                return true;
            default:
                //Cancel pushed
                return false;
        }
    }

    private class EditPanel extends JPanel implements ActionListener {

        Date date;
        JDateChooser dc;
        String newCompare = compare;

        private EditPanel(Date date) {
            this.date = date;
            init();
        }

        public JDateChooser getDc() {
            return dc;
        }

        public String getNewCompare() {
            return newCompare;
        }

        public void init() {
           add(createMainPanel());
        }


        private JPanel createMainPanel() {
            JPanel mainPanel = new JPanel(new GridBagLayout());

            JRadioButton eqButton = new JRadioButton("=");
            eqButton.setActionCommand("=");
            if(compare.equals("=")) {
                eqButton.setSelected(true);
            }

            JRadioButton beforeButton = new JRadioButton(getTranslation().getLabelBefore());
            beforeButton.setActionCommand("<");
            if(compare.equals("<")) {
                beforeButton.setSelected(true);
            }

            JRadioButton afterButton = new JRadioButton(getTranslation().getLabelAfter());
            afterButton.setActionCommand(">");
            if(compare.equals(">")) {
                afterButton.setSelected(true);
            }

            //Group the radio buttons.
            ButtonGroup group = new ButtonGroup();
            group.add(eqButton);
            group.add(beforeButton);
            group.add(afterButton);

            eqButton.addActionListener(this);
            beforeButton.addActionListener(this);
            afterButton.addActionListener(this);

            JPanel radioPanel = new JPanel(new GridLayout());
            radioPanel.add(eqButton);
            radioPanel.add(beforeButton);
            radioPanel.add(afterButton);

            GridBagConstraints c = new GridBagConstraints();
            c.anchor = GridBagConstraints.NORTHWEST;
            c.weightx = 0; // add horizontal space to this component
            c.weighty = 0; // add vertical space to this component
            c.gridwidth = GridBagConstraints.REMAINDER; // end of row
            c.gridheight = 1;
            c.fill = GridBagConstraints.NONE; // fill vertically and horizontally if bigger than expected

            mainPanel.add(radioPanel, c);

            dc = new JDateChooser(date);

            //TODO make configurable
            dc.setLocale(new Locale("sv", "SE"));
            c.anchor = GridBagConstraints.CENTER;
            c.weightx = 1; // add horizontal space to this component
            c.weighty = 1; // add vertical space to this component
            c.gridwidth = GridBagConstraints.REMAINDER; // end of row
            c.gridheight = 1;
            c.fill = GridBagConstraints.HORIZONTAL; // fill vertically and horizontally if bigger than expected
            mainPanel.add(dc, c);

            JPanel filler = new JPanel();
            c.anchor = GridBagConstraints.NORTHWEST;
            c.weightx = 1; // add horizontal space to this component
            c.weighty = 1; // add vertical space to this component
            c.gridwidth = 1; // end of row
            c.gridheight = 1;
            c.fill = GridBagConstraints.BOTH; // fill vertically and horizontally if bigger than expected
            mainPanel.add(filler, c);

            return mainPanel;
        }

        public void actionPerformed(ActionEvent event) {
            newCompare = event.getActionCommand();
        }
    }
}
